package com.catering.controller;

import com.catering.pojo.DEmployeeUser;
import com.catering.pojo.Employee;
import com.catering.pojo.VO.Result;
import com.catering.service.LoginService;
import com.catering.untils.AppJwtUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("employee")
public class LoginController {

    @Resource
    private LoginService loginService;
//    @RequestMapping("login")
//    public Result login(@RequestBody DEmployeeUser dEmployeeUser){
//        DEmployeeUser dEmployeeUser1=loginService.login(dEmployeeUser.getUserID(),dEmployeeUser.getPassword());
//        if (dEmployeeUser1==null){
//            return Result.ERR("用户名或密码错误", null);
//        }else{
//            return Result.OK("登陆成功",dEmployeeUser1);
//        }
//    }
//        //登录
//    @RequestMapping("/login")
//    public Result login(@RequestBody DEmployeeUser dEmployeeUser){
//        DEmployeeUser users= loginService.login(dEmployeeUser.getUsername(),dEmployeeUser.getPassword());
//        String token = AppJwtUtil.getToken(users.getId().longValue());
//        Map<String, Object> map = new HashMap<>();
//        map.put("token", token);
//        return Result.OK("登录成功",map);
//}

    // 登录接口
    @PostMapping("/login")
    public Result login(@RequestBody DEmployeeUser dEmployeeUser) {
        if (dEmployeeUser == null || StringUtils.isBlank(dEmployeeUser.getUsername()) || StringUtils.isBlank(dEmployeeUser.getPassword())) {
            return Result.ERR("用户名或密码不能为空", null);
        }
        DEmployeeUser users = loginService.login(dEmployeeUser.getUsername(), dEmployeeUser.getPassword());
        if (users == null) {
            return Result.ERR("用户名或密码错误", null);
        }
        String token = AppJwtUtil.getToken(users.getId().longValue());
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.OK("登录成功", map);
    }

    //注册
    @RequestMapping("/register")
    public Result register(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("phone") String phone){
        DEmployeeUser users= loginService.register(username,password,phone);
        return Result.OK("注册成功",users);
    }



}
